package ru.papergames.battleserver.model.battle;

import java.util.ArrayList;

/**
 * User: troshanin
 * Date: 27.02.2011
 * Time: 14:57:22
 */
public class BattlePlayersSequenceController {

    private BattlePlayer[] playersSequence;
    private int position;

    public synchronized void init(ArrayList<BattlePlayer> battleMembers) {
        playersSequence = battleMembers.toArray(new BattlePlayer[battleMembers.size()]);
        position = -1;
    }

    public synchronized BattlePlayer getBattlePlayerForNextTurn() {
        int turn = position;
        int seqLen = playersSequence.length;
        for (int i = 0; i < seqLen; i++) {
            turn++;
            turn = (turn >= playersSequence.length) ? 0 : turn;
            BattlePlayer player = playersSequence[turn];
            if (player != null && !player.isDead() && player.isConnected()) {
//            if (player != null) {
                position = turn;
                return player;
            }
        }
        return null;
    }

}
